﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents;
using WebComponents.Security;
using GBIT.Web;

public partial class ThemTinTuc : UserControlBase {
  int articleID = 0;
  protected void Page_Load(object sender, EventArgs e) {
    //txtTrichDan.ToolbarStartExpanded = false;
    this.ObjectManagerModule = "TinTuc/TinTucList.ascx";
    this.Title = "Bài viết";
    this.TaskTitle = "Thêm mới";
    if (Request.Params["id"] != null)
      articleID = ConvertType.ToInt(Request.Params["id"]);
    if (!Page.IsPostBack) {
      cmbChuyenMuc.Items.Clear();
      cmbChuyenMuc.Items.Add(new ListItem("", ""));
      Global.LoadCategory(cmbLanguage.SelectedValue, ref cmbChuyenMuc, CatetoryType.News);
      if (articleID > 0) {
        ArticleInfo article = ArticleController.GetByArticleID(articleID);
        if (article != null) {
          txtTitle.Text = article.Title;
          txtTrichDan.Text = article.Description;
          txtContent.Value = article.Content;
          txtOrderId.Text = article.OrderId.ToString();
          ImagePath.ImagePath = article.ImageUrl;
          ImagePath.ImageUrl = article.ImageUrl;        

          ImagePathSlideShow.ImagePath = article.ImageDescription;
          ImagePathSlideShow.ImageUrl = article.ImageDescription;         

          chkIsActive.Checked = article.IsApproved;
          if (this.Action == "copy")
            this.TaskTitle = "Sao chép";
          else {
            this.TaskTitle = "Cập nhật";
            this.ApplyEnabled = true;
          }
          if (article.StatusID == 2)
            chkSlideShow.Checked = true;
          else
            chkSlideShow.Checked = false;
          chkNoiBat.Checked = article.OnlyForMembers;
          cmbChuyenMuc.SelectedIndex = -1;
          if (cmbChuyenMuc.Items.FindByValue(article.CateID.ToString()) != null)
            cmbChuyenMuc.Items.FindByValue(article.CateID.ToString()).Selected = true;
          if (article.CultureId == "vi-VN")
            cmbLanguage.SelectedIndex = 0;
          else
            cmbLanguage.SelectedIndex = 1;          
        }
      }
    }
    this.SaveEnabled = true;
  }
  protected override void OnSaveRecord(bool redirect) {
    ArticleInfo article = null;
    if (articleID > 0)
      article = ArticleController.GetByArticleID(articleID);
    if (article == null)
      article = new ArticleInfo();
    article.Title = txtTitle.Text;
    article.Description = txtTrichDan.Text;
    article.Content = txtContent.Value;
    article.OrderId = ConvertType.ToInt(txtOrderId.Text);
    article.ImageUrl = ImagePath.ImagePath;
    article.IsApproved = chkIsActive.Checked;
    article.CultureId = cmbLanguage.SelectedValue;
    //article.StatusID = ConvertType.ToInt(cmbStatus.SelectedValue);   
    article.StatusID = (chkSlideShow.Checked) ? 2 : 1;
    article.OnlyForMembers = chkNoiBat.Checked;
    article.ImageDescription = ImagePathSlideShow.ImagePath;
    article.CateID = ToInt(cmbChuyenMuc.SelectedValue);
    if (articleID <= 0 || this.Action == "copy") {
      article.AddedDate = DateTime.Now;
      article.CateID = ToInt(cmbChuyenMuc.SelectedValue);
      ArticleController.Insert(article);
      if (redirect)
        Security.ShowClientMessage(Page, "Thêm tin mới thành công", string.Format("?mod={0}", WebComponents.ConvertType.Encode("TinTuc/TinTucList.ascx")));
      else {
        Security.ShowClientMessage(Page, "Thêm tin mới thành công");
        this.ApplyEnabled = true;
      }
    } else {
      article.UpdatedDate = DateTime.Now;
      ArticleController.Update(article);
      if (redirect)
        Security.ShowClientMessage(Page, "Cập nhật thông tin thành công", string.Format("?mod={0}", WebComponents.ConvertType.Encode("TinTuc/TinTucList.ascx")));
      else {
        Security.ShowClientMessage(Page, "Cập nhật thông tin thành công");
        this.ApplyEnabled = true;
      }
    }
  }
  protected void cmbChuyenMuc_SelectedIndexChanged(object sender, EventArgs e) {
    if (cmbChuyenMuc.SelectedIndex == 1 || cmbChuyenMuc.SelectedIndex == 4) {
      slideShow.Visible = false;
      vunghienthi.Visible = false;
    } else {
      slideShow.Visible = true;
      vunghienthi.Visible = true;
    }
  }
}
